package com.sys.console.action;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.console.business.SysModuleService;
import com.sys.console.business.SysRoleService;
import com.sys.console.pojo.RoleMeunJsonDto;
import com.sys.console.pojo.Sys_Module;
import com.sys.console.pojo.Sys_Role;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;


/**
 * 2014-11-05
 * 
 * @author 陈成军 角色管理 action
 */
@Controller
public class SysRoleAction {

	@Resource SysRoleService sysRoleService;
	@Resource SysModuleService sysModuleService;

	/**
	 * 角色列表
	 */
	@RequestMapping(value = "/sysRoleList.do")
	public String dynamicList(PageBean pageBean, Model model,Sys_Role role) {

		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			pageBean = sysRoleService.queryPageList(role, pageBean);
			model.addAttribute("pageBean", pageBean);
			model.addAttribute("startRow", pageBean.getStartRow());
			model.addAttribute("entity", role);
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", ret);
		}

		return "/system/role/list";
	}

	/**
	 * 新增角色
	 */
	@RequestMapping(value = "/addSysRole.do", method = RequestMethod.GET)
	public String addSysRole(Sys_Role role, Model model) {
		try {
			if(role.getRid()!=null){
				Sys_Role record = sysRoleService.queryByPrimaryKey(role.getRid());
				model.addAttribute("role", record);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/system/role/add";
	}
	
	/**
	 * 新增角色
	 */
	@RequestMapping(value = "/addSysRoleByLayer.do", method = RequestMethod.GET)
	public String addSysRoleByLayer(Sys_Role role, Model model) {
		try {
			if(role.getRid()!=null){
				Sys_Role record = sysRoleService.queryByPrimaryKey(role.getRid());
				model.addAttribute("role", record);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/system/role/addLayer";
	}

	/**
	 * 新增角色
	 */
	@RequestMapping(value = "/saveSysRole.do", method = RequestMethod.POST)
	public String saveSysRole(@RequestParam("sysRole") Sys_Role sysRole,Model model) {

		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = sysRoleService.saveOrUpdate(sysRole);

			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.ROLE_ADD_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return "redirect:/sysRoleList.do";
	}
	
	/**
	 * 新增角色
	 */
	@RequestMapping(value = "/saveSysRoleJson.do", method = RequestMethod.POST)
	@ResponseBody
	public ObjResultJSON<Sys_Role> saveSysRoleJson(@RequestBody Sys_Role sysRole,Model model) {
		ObjResultJSON<Sys_Role> ret = new ObjResultJSON<Sys_Role>();
		try {
			int success = sysRoleService.saveOrUpdate(sysRole);
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(sysRole);
			} else {
				ret.setResultCode(ObjResultCode.ROLE_ADD_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	/**
	 * 删除
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping("/deleteSysRole.do")
	@ResponseBody
	public ObjResultException<String> roleAjaxDelete(int id) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = sysRoleService.delete(id);

			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.ROLE_DELETE_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/sysRoleAjaxList.do")
	@ResponseBody
	public ObjResultJSON<RoleMeunJsonDto> sysRoleAuthorityAjaxList(HttpServletRequest request,HttpServletResponse response, Model model,Integer rid, Integer mid) {
		
		ObjResultJSON<RoleMeunJsonDto> retjson = new  ObjResultJSON<RoleMeunJsonDto>();
		try {
			RoleMeunJsonDto dto = new RoleMeunJsonDto();
			Sys_Module module = new Sys_Module();
			module.setPid(0);
			ArrayList<Sys_Role> list = sysRoleService.selectByList();
			ArrayList<Sys_Module> modulelist = sysModuleService.selectByPid(module);
			dto.setRolelist(list);
			dto.setModulelist(modulelist);
			if(list!=null&&list.size()>0){
				retjson.setData(dto);
				retjson.setResultCode(ObjResultCode.OK);
			}else{
				retjson.setResultCode(ObjResultCode.DATA_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				retjson.setResultCode(((ObjException) e).getCode());
			} else {
				retjson.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", retjson);
		}
		return retjson;
	}
	
}
